package org.br.plotter.functions;

import java.util.List;

public class EmbededFunction extends Function {
	private Function[] params;
	private EmbededFunctionExecuter executer;
	private double[] funcParams;
	int length;

	public EmbededFunction(String functionName, Function[] params) {
		this.params = params;
		this.executer = FunctionLocator.getFunction(functionName);
		if (executer == null) {
			FunctionLocator.getExceptionHandler().handleException(
					new EvaluatorException("Function called:" + functionName
							+ " is not supported yet"));
		}
		length = params.length;
		funcParams = new double[length];
	}


	public EmbededFunction(String functionName, List<Function> params) {
		this(functionName, toArray(params));
	}

	private static Function[] toArray(List<Function> functions) {
		int size = functions.size();
		Function[] result = new Function[size];
		for (int i = 0; i < size; i++) {
			result[i] = functions.get(i);
		}
		return result;
	}

	public double eval(double x) {
		for (int i = 0; i < length; i++) {
			funcParams[i] = params[i].eval(x);
		}
		return executer.executeFunction(funcParams);
	}

}